package com.apartment.own.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.apartment.common.annotation.CacheClear;
import com.apartment.common.annotation.CacheSelect;
import com.apartment.common.core.domain.model.LoginUser;
import com.apartment.common.core.page.PageDomain;
import com.apartment.common.utils.DateUtils;
import com.apartment.common.utils.SecurityUtils;
import com.apartment.own.domain.OwnResHouseRoomConf;
import com.apartment.own.request.workorder.OwnSysWorkOrderQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.apartment.own.mapper.OwnSysWorkOrderMapper;
import com.apartment.own.domain.OwnSysWorkOrder;
import com.apartment.own.service.IOwnSysWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修订单Service业务层处理
 *
 * @author @Dog_E
 * @date 2020-08-28
 */
@Service
@Transactional
public class OwnSysWorkOrderServiceImpl extends ServiceImpl<OwnSysWorkOrderMapper, OwnSysWorkOrder> implements IOwnSysWorkOrderService {
    @Autowired
    private OwnSysWorkOrderMapper ownSysWorkOrderMapper;

    /**
     * 查询维修订单
     *
     * @param id 维修订单ID
     * @return 维修订单
     */
    @Override
    @CacheSelect
    public OwnSysWorkOrder selectOwnSysWorkOrderById(String id) {
        QueryWrapper<OwnSysWorkOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return ownSysWorkOrderMapper.selectOne(wrapper);
    }

    /**
     * 查询维修订单列表
     *
     * @param
     * @return 维修订单
     */
    @Override
    @CacheSelect
    public List<OwnSysWorkOrder> selectOwnSysWorkOrderList(OwnSysWorkOrderQuery query) {
        QueryWrapper<OwnSysWorkOrder> wrapper = new QueryWrapper<>();
        if (query != null) {
            wrapper
                    //-------------------多条件搜索--------------------
                    .and(query.getManySearch() != null, x -> x
                            //根据房源编号搜索
                            .or().eq("room.id", query.getManySearch())
                            //根据姓名搜索
                            .or().like("worked.repair_user", query.getManySearch())
                            //根据小区名搜索
                            .or().like("houses.housing_estate", query.getManySearch())
                            //根据电话搜索
                            .or().eq("worked.user_phone_number", query.getManySearch())
                    )
                    //---------------------end----------------------------
                    //根据小区名查询
                    .in((query.getHouseEstate() != null), "houses.housing_estate", query.getHouseEstate())
                    //根据城市id查询
                    .in((query.getCityId() != null), "houses.city_id", query.getCityId())
                    //根据员工名搜索
                    .eq((query.getStafferName() != null), "worked.create_by", query.getStafferName())
                    //根据紧急程度搜索
                    .eq((query.getEmergencyDegree() != null && query.getEmergencyDegree() != 0), "worked.emergency_degree", query.getEmergencyDegree())
                    //根据任务状态查询
                    .eq((query.getTaskStatus() != null), "worked.task_status", query.getTaskStatus())
                    //根据整租合租查询
                    .eq((query.getLeaseModel() != null), "room.lease_model", query.getLeaseModel());
        }
        List<OwnSysWorkOrder> list = ownSysWorkOrderMapper.selectOwnSysWorkOrderLists(wrapper);
        return list;
    }

    /**
     * 新增维修订单
     *
     * @param ownSysWorkOrder 维修订单
     * @return 结果
     */
    @Override
    @CacheClear
    public int insertOwnSysWorkOrder(OwnSysWorkOrderQuery ownSysWorkOrder) {
        //添加创建人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        ownSysWorkOrder.setCreateBy(username);
        save(ownSysWorkOrder);
        return 1;
    }

    /**
     * 修改维修订单
     *
     * @param ownSysWorkOrder 维修订单
     * @return 结果
     */
    @Override
    @CacheClear
    public int updateOwnSysWorkOrder(OwnSysWorkOrder ownSysWorkOrder) {
        ownSysWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return ownSysWorkOrderMapper.updateOwnSysWorkOrder(ownSysWorkOrder);
    }

    /**
     * 批量删除维修订单
     *
     * @param ids 需要删除的维修订单ID
     * @return 结果
     */
    @Override
    @CacheClear
    public int deleteOwnSysWorkOrderByIds(String[] ids) {
        List<String> collect = Arrays.stream(ids).collect(Collectors.toList());
        return ownSysWorkOrderMapper.deleteBatchIds(collect);
    }

    /**
     * 删除维修订单信息
     *
     * @param id 维修订单ID
     * @return 结果
     */
    @Override
    @CacheClear
    public int deleteOwnSysWorkOrderById(String id) {
        return ownSysWorkOrderMapper.deleteOwnSysWorkOrderById(id);
    }
}
